package com.study.samsung.Control.htControl;

import com.alibaba.fastjson.JSONObject;
import com.study.samsung.Entity.htEntity.Admin;
import com.study.samsung.Service.htService.AdminService;
import com.study.samsung.Utils.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/message")
@MultipartConfig
public class MessageServlet extends HttpServlet {

    AdminService service = new AdminService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Object obj = session.getAttribute("admin");
        if (obj==null){
            resp.sendRedirect("/htlogin");
        }else {
            req.getRequestDispatcher("/WEB-INF/jsp/htjsp/Message.jsp")
                    .forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        System.out.println("我到了你这个方法");
        HttpSession session = req.getSession();
        Admin admin = new Admin();
        JSONObject json = new JSONObject();


        String uname = req.getParameter("uname");
        System.out.println("uname="+uname);


        String pass  = req.getParameter("pass") ;
        System.out.println("pass="+pass);

        Object obj = session.getAttribute("admin");
        System.out.println("obj="+obj);

        if (obj==null){
            json.put("code",1);
        }else {
            Admin sessionAdmin = (Admin)obj;
            sessionAdmin.setAdminaccount(uname);
            sessionAdmin.setAdminpwd(pass);
            service.doUpdate(sessionAdmin);
            json.put("code",1);
            json.put("adminaccount",sessionAdmin.getAdminaccount());
        }
        PrintWriter out = resp.getWriter();
        out.print(json.toJSONString());
        out.close();
    }
}
